<?php


namespace app\util;


use app\exception\InputValidateException;
use think\exception\ValidateException;

class Post
{
    /**
     * @param $validate mixed 验证器
     * @param $request mixed request对象
     * @param $isCamelize boolean 是否返回驼峰
     * @return array
     * @throws InputValidateException
     */
    public static function getParams($validate, $request, $isCamelize = true) {
        $class = new $validate();
        $data = [];
        foreach ($class->getRule() as $key => $value) {
            $data[$key] = $request->post($key);
        }
        try {
            validate($validate)->check($data);
        } catch (ValidateException $e) {
            throw new InputValidateException([
                'message' => $e->getMessage()
            ]);
        }
        if (!$isCamelize) {
            return Camelize::uncamelizeArray($data);
        }
        return $data;
    }
}